package shopping.cart;  
  
import java.util.ArrayList;  
import java.util.Iterator;  
import java.util.List;  
  
  
public class Cart {  
    List<GoodsItem> list = new ArrayList<GoodsItem>();  
  
    public List<GoodsItem> getItems() {  
        return list;  
    }  
  
    public void setItems(List<GoodsItem> items) {  
        this.list = items;  
    }  
      
    public void add(GoodsItem gi) {  
        for (Iterator<GoodsItem> it = list.iterator(); it.hasNext();) {  
        	GoodsItem item = it.next();  
            if(item.getGoods().getId() == gi.getGoods().getId()) {  
                item.setCount(item.getCount() + 1);  
                return;  
            }  
        }   
        list.add(gi);  
    }  
    
    public void clear(){
    	list.clear();
    }
      
    public double getTotalPrice() {  
        double sum = 0.0;  
        for(Iterator<GoodsItem> it = list.iterator(); it.hasNext(); ) {  
        	GoodsItem gi = it.next();  
            sum+= gi.getGoods().getPrice() * gi.getCount();  
        }  
        return sum;  
    }  
      
    public void deleteItemById(String productId) {  
        for (Iterator<GoodsItem> it = list.iterator(); it.hasNext();) {  
        	GoodsItem item = it.next();  
            if(item.getGoods().getId().equals(productId)) {  
                it.remove();  
                return;  
            }  
        }  
    }  
      
}  